import { DirectiveBinding } from 'vue';
import { wmsStore } from '@/store/pinia'

const hasRoles = (needRoles: any) => {
    if (needRoles == undefined)
        return true;

    //判断是否拥有角色
    const store = wmsStore()
    let piniaRoles = store.roles;
    if (Array.isArray(needRoles)) {

        return needRoles.some((role: any) => piniaRoles.includes(role))
    }
    else if (typeof needRoles == "string") {
        return piniaRoles.includes(needRoles);
    } else {
        return false
    }

}
const hasPermission = (needPermission: any) => {
    //判断是否拥有角色
    const store = wmsStore()
    let piniaPermissions = store.permissions;
    if (Array.isArray(needPermission)) {

        return needPermission.some((role: any) => piniaPermissions.includes(role))
    }
    else if (typeof needPermission == "string") {
        return piniaPermissions.includes(needPermission);
    } else {
        return false
    }

}
export default {
    // 当被绑定的元素插入到 DOM 中时……
    mounted(el: HTMLElement, binding: DirectiveBinding) {

        let type = binding.arg
        let value = binding.value
        if (type == "role") {
            //判断是否拥有角色
            if (!hasRoles(value))
                el.remove()

        } else {
            //判断是否拥有权限
            if (!hasPermission(value))
                el.remove()
        }

    }
};